﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Lon.UI.Station
{
    public partial class FormMessageOut : WeifenLuo.WinFormsUI.Docking.DockContent
    {
       const int messageIndex=0;
       List<DisplayMessage> displayMessageList = null;
       public FormMessageOut(List<DisplayMessage> displayMessageList)
        {
            InitializeComponent();
            this.displayMessageList = displayMessageList;
            mBoundList = new DevAge.ComponentModel.BoundList<DisplayMessage>(displayMessageList);
             mBoundList.ListChanged += new ListChangedEventHandler(mBoundList_ListChanged);
            dataGrid1.Columns.Add("Time", "时间",
                                typeof(DateTime)).Width = 100;
            dataGrid1.Columns.Add("Message", "消息",
                                typeof(string)).Width = this.Width - 101;
            SourceGrid.Cells.Editors.TextBox editorCustom = new SourceGrid.Cells.Editors.TextBox(typeof(double));
            DevAge.ComponentModel.Converter.DateTimeTypeConverter numberFormatCustom = new DevAge.ComponentModel.Converter.DateTimeTypeConverter("HH:mm:ss.fff");
            editorCustom.TypeConverter = numberFormatCustom;
            dataGrid1.Columns[0].DataCell.Editor= editorCustom;
            dataGrid1.DataSource = mBoundList;

            dataGrid1.Columns.StretchToFit();
           // dataGrid1.Rows.AutoSize(false);
            dataGrid1.Rows.StretchToFit();
            dataGrid1.AutoSize = true;
            
            this.SizeChanged += new System.EventHandler(this.FormMessageOut_SizeChanged);
        }

       void mBoundList_ListChanged(object sender, ListChangedEventArgs e)
       {

           //throw new NotImplementedException();
       }
        private DevAge.ComponentModel.BoundList<DisplayMessage> mBoundList;

        private void FormMessageOut_Load(object sender, EventArgs e)
        {
            dataGrid1.Invalidate();
            dataGrid1.Columns[0].Width = 100;
            dataGrid1.Columns[1].Width = this.Width - 101;
        }
      

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            displayMessageList.Clear();
            mBoundList.RemovedItems.Clear();
            dataGrid1.Invalidate();
            
        }
    
        public new void Show()
        {
            if (FormMain.GlobalPanel!=null)
            {
                base.Show(FormMain.GlobalPanel);
             
            }
            else
            {
                base.Show();
            }
            dataGrid1.Columns[0].Width = 100;
            dataGrid1.Columns[1].Width = this.Width - 101;
        }

        private void FormMessageOut_SizeChanged(object sender, EventArgs e)
        {
            dataGrid1.Columns[0].Width = 100;
            dataGrid1.Columns[1].Width = this.Width - 101;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (lastRowCount == displayMessageList.Count)
            {
                return;
            }
            lastRowCount = displayMessageList.Count;
            dataGrid1.Invalidate();
            dataGrid1.RecalcCustomScrollBars();
         
            if (gotoLastRow)
                dataGrid1.Selection.Focus(new SourceGrid.Position(dataGrid1.Rows.Count-1, 0), true);
        }
        bool gotoLastRow = true;
        int lastRowCount = 0;
        private void toolStripButton4_Click(object sender, EventArgs e)
        {
          
            if (gotoLastRow == false)
            {
                gotoLastRow = true;
                toolStripButton4.BackColor = Color.LimeGreen;

            }
            else
            {
                gotoLastRow = false;
                toolStripButton4.BackColor = Color.Red;

            }
        }
    }

    public class DisplayMessage
    {
        private DateTime time;

        public DateTime Time
        {
            get { return time; }
            set { time = value; }
        }
        private String message;

        public String Message
        {
            get { return message; }
            set { message = value; }
        }
       
    }
}
